Dynamic Dispatching

Primitive operations that are inherited by a type extension can be redefined, in which case the new definition overrides the old one. When a primitive operation of the root type is applied to a classwide argument, the tag of the argument determines the implementation of the operation which is to be executed, i.e. the original operation or one of its overridings. The tag is implemented as a pointer to a dispatch table. The table contains pointers to the primitive operations of the type. There is one table for each tagged type, and the layout of all types in a derivation class is compatible, in the sense that different overridings of the same operation appear in the same table position. Note that in Ada terms this table is not an array, because each component is an access to a subprogram with a different profile. A call to a primitive operation is dispatching if the specific type of its tagged arguments cannot be determined statically. In such a case, the tag of one of the actuals is chosen to determine which subprogram to call. Simplifying somewhat,if we consider type <#742#>R1<#742#> introduced in section~#inher#743> and we encounter 114

Primit_n (Param: R1'Class)
115 then dispatching amounts to replacing:

<#745#>Primit_n (X)<#745#>         with         <#746#>X._tag.all.Access_Primit_n.all (X)<#746#>
The layout of the dispatch table is shown in Figure~#c2#747>. The first two components of the table simplify the implementation of the membership operation for tagged types.

<#2775#>Figure<#2775#>: <#2776#><#2776#>
#figure748#